package com.huateng.util;

import com.google.common.collect.Lists;
import com.huateng.vo.ResponseVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by joy on 2016/11/6.
 */
@Component
public class RedisUtils {
    private static Logger logger = Logger.getLogger(RedisUtils.class);
    @Resource
    public JedisPool jedisPool;
    @Resource
    private SecureUtils secureUtils;


    public ResponseVo getPageCache(String key){
        Jedis jedis = jedisPool.getResource();
        byte[] bytes= jedis.get(key.getBytes());
        ResponseVo vo = null;
        try {
             vo = ObjectsTranscoder.deserialize(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
        }finally {
            CloseUitl.close(jedis);
        }
        return vo;
    }

    public void putPageCache(String key,ResponseVo vo){
        Jedis jedis = jedisPool.getResource();;
        try {
            byte[] bytes = ObjectsTranscoder.serialize(vo);
            jedis.set(key.getBytes(),bytes);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
        }finally {
            CloseUitl.close(jedis);
        }
    }

    static class ObjectsTranscoder{
        public static byte[] serialize(ResponseVo value) throws IOException {
            if (value == null) {
                throw new NullPointerException("Can't serialize null");
            }
            byte[] rv=null;
            ByteArrayOutputStream bos = null;
            ObjectOutputStream os = null;
            try {
                bos = new ByteArrayOutputStream();
                os = new ObjectOutputStream(bos);
                os.writeObject(value);
                os.close();
                bos.close();
                rv = bos.toByteArray();
            } finally {
                CloseUitl.close(os);
                CloseUitl.close(bos);
            }
            return rv;
        }

        public static ResponseVo deserialize(byte[] in) throws IOException, ClassNotFoundException {
            ByteArrayInputStream bis = null;
            ObjectInputStream is = null;
            ResponseVo vo = null;
            try {
                if(in != null) {
                    bis=new ByteArrayInputStream(in);
                    is=new ObjectInputStream(bis);
                    vo = (ResponseVo) is.readObject();
                }
            } finally {
                CloseUitl.close(is);
                CloseUitl.close(bis);
            }
            return vo;
        }
    }
}
